//
//  PopDetailsViewController.m
//  SugarPile
//
//  Created by Charlie. on 16/1/28.
//  Copyright © 2016年 www.lanou39.蓝鸥科技. All rights reserved.
//

#import "PopDetailsViewController.h"
#import "LCPrefixHeader.pch"
#import "HotModel.h"
#import "HotCollectionViewCell.h"
@interface PopDetailsViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, LCLayoutItemHeightDelegate>
@property (nonatomic, retain) NSMutableArray *dataArr; /**< 处理数据 */

@property (nonatomic, retain) UICollectionView *collectionView; /**< 瀑布流Collection*/
@property (nonatomic, copy) NSString *strURL; /**<用于接受拼接好字符串*/
@end

@implementation PopDetailsViewController

- (void)dealloc
{
    [_popId release];
    [_popWebURL release];
    [_dataArr release];
    [_collectionView release];
    [_strURL release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 判断接受数据进行下一步操作
    // URL有值得时候 执行方法
    if (_popWebURL) {
        UIWebView * store = [[UIWebView alloc] initWithFrame:self.view.bounds];
        self.navigationController.navigationBar.tintColor = [UIColor redColor                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            ];
        store.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:store];
        [store loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_popWebURL]]];
        [store release];

        
    // Id 有值得时候执行另外一种方法
    }else if (_popId){
        // 创建瀑布流
        [self createCollection];
        // 解析数据
        [self dataArrHandle];
    }
    
}
#pragma mark - 数据解析
- (void)dataArrHandle
{
    NSString *str = @"http://203.80.144.212/napi/blog/list/by_filter_id/?include_fields=sender%2Calbum%2Cicon_url%2Clike_count%2Creply_count&platform_version=4.4.4&filter_id=%E7%BE%8E%E9%A3%9F%E8%8F%9C%E8%B0%B1_%E7%81%AB%E9%94%85&device_platform=HM%2B2A&__dtac=%257B%2522_r%2522%253A%2520%2522511687%2522%257D&screen_width=720&screen_height=1280&__domain=www.duitang.com&app_version=68&start=0&platform_name=Android&locale=zh&app_code=nayutas";
    self.strURL = [str stringByReplacingOccurrencesOfString:@"id=%E7%BE%8E%E9%A3%9F%E8%8F%9C%E8%B0%B1_%E7%81%AB%E9%94%85" withString:[NSString stringWithFormat:@"id=%@", _popId]];
    [LCAFNetWorking GETWithURL:_strURL completion:^(id result) {
       
        self.dataArr = [HotModel modelHanderWithArray:result[@"data"][@"object_list"]];
        // 刷新
        [_collectionView reloadData];
    }];
}

#pragma mark - 创建Collcetion

- (void)createCollection
{
    LClayout *layout = [[LClayout alloc] init];
    layout.columcounts = 2;
    layout.columSpace = 15;
    layout.rowSpace = 15;
    layout.edgeInsets = UIEdgeInsetsMake(15, 15, 15, 15);
    // 代理人
    layout.delegate = self;
    // coleation 属性
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    // 协议
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    _collectionView.backgroundColor = [UIColor floralWhite];
    // 重用池
    [_collectionView registerClass:[HotCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

    [_collectionView release];
    [layout release];

}
#pragma mark - Collection
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return _dataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HotCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    if (self.dataArr.count != 0) {
        
        HotModel *m = _dataArr[indexPath.item];
        //        self.index = indexPath;
        // m 传入cell;
        cell.model = m;
    }
    return cell;
}
#pragma mark - layout协议属性
-(CGFloat)layout:(LClayout *)layout heightForItemAtindexPath:(NSIndexPath *)indexPath width:(CGFloat)width
{
    // tw/wh = iw/ih
    
    HotModel *m = _dataArr[indexPath.item];
    CGFloat h = width * m.height.doubleValue / m.width.doubleValue;
    
    //文本高度
    CGFloat textHeight = [HotCollectionViewCell heightWithText:m.msg Size:9];
    //判断文本框 高度是否 超过 15.51 * 6 的 高度(6行的高度)
    if (textHeight >= 15.51 * 6) {
        //设置最大文本框行数 为 6行;
        textHeight = 15.51 * 6;
    }
    // 每个cell的高度 由 图片高度 和 文本高度  + 其他控件高度(80)
    return h + textHeight + 80;
}
- (void)array:(NSMutableArray *)arr
{
//    if (_dataArr.count == 0) {
//        [arr removeAllObjects];
//    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
